package com.flyou.ccnucenter.ui.frgment.HenuImage;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.flyou.ccnucenter.Constant;
import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.adapter.HenuImageAdapter;
import com.flyou.ccnucenter.domain.HenuImage;
import com.flyou.ccnucenter.ui.activity.imageDetail.ImageDetailActivity;
import com.flyou.ccnucenter.ui.frgment.BaseFragment;
import com.flyou.ccnucenter.utils.MyLog;
import com.flyou.ccnucenter.utils.SPUtils;
import com.flyou.ccnucenter.utils.okhttp.callback.ResultCallback;
import com.flyou.ccnucenter.utils.okhttp.request.OkHttpRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.okhttp.Request;
import com.yalantis.phoenix.PullToRefreshView;

/**
 * ============================================================
 * 项目名称：HenuCenterPro
 * 包名称：com.flyou.flying.ui.frgment.HenuImage
 * 文件名：Summer
 * 类描述：
 * 创建人：flyou
 * 邮箱：fangjaylong@gmail.com
 * 创建时间：2015/9/28 11:37
 * 修改备注：
 * 版本：@version  V1.0
 * ============================================================
 */
public class Summer extends BaseFragment {
    private HenuImageAdapter adapter;
    private String mClassName;
    private View mPaentView;
    private PullToRefreshView mPullToRefreshView;
    private ListView listView;
    private HenuImage henuImages;

    @Override
    protected View onCreatView(LayoutInflater inflater, ViewGroup container) {
        Fresco.initialize(context);
        mPaentView = inflater.inflate(R.layout.image_fragment_layout, container, false);
        return mPaentView;
    }

    @Override
    protected void initData() {
        mClassName = getClass().getSimpleName();
        String jsonString = (String) SPUtils.get(context, mClassName, "");
        if (!TextUtils.isEmpty(jsonString)) {
            Gson gson = new Gson();
            henuImages = gson.fromJson(jsonString, new TypeToken<HenuImage>() {
            }.getType());
            adapter = new HenuImageAdapter(context, henuImages.getImageThumbnail());
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(onItemClickListener);
            MyLog.d("从本地获取");
        } else {
            GetImageDataFromNet();
        }

    }

    @Override
    protected void initView() {
        mPullToRefreshView = (PullToRefreshView) mPaentView.findViewById(R.id.pull_to_refresh);
        listView = (ListView) mPaentView.findViewById(R.id.list_view);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                GetImageDataFromNet();
            }
        });


    }


    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(context, ImageDetailActivity.class);
            intent.putExtra(Constant.HENU_ITEM_IMAGE_ICON_URL, henuImages.getImageThumbnail().get(position));
            intent.putExtra(Constant.HENU_ITEM_IMAGE_URL, henuImages.getImage().get(position));
            startActivity(intent);
        }
    };

    private void GetImageDataFromNet() {
        new OkHttpRequest.Builder()
                .url(Constant.HENU_IMAGE_SUMMER)
                .get(new ResultCallback<HenuImage>() {
                    @Override
                    public void onAfter() {
                        super.onAfter();
                        if (mPullToRefreshView.isShown()) {
                            mPullToRefreshView.setRefreshing(false);
                        }
                    }

                    @Override
                    public void onError(Request request, Exception e) {
                        Toast.makeText(context, "我在校园里迷路了，请检查网络", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(HenuImage response) {

                        henuImages = response;
                        adapter = new HenuImageAdapter(context, response.getImageThumbnail());
                        listView.setAdapter(adapter);
                        listView.setOnItemClickListener(onItemClickListener);

                        String json = toJson(response);
                        SPUtils.put(context, mClassName, json);


                    }
                });
    }
}